import 'package:dmz/base/pageWidget/base_stateful_widget.dart';
import 'package:dmz/base/widget/defult_widget.dart';
import 'package:dmz/business/mine/controller/lost_updata_controller.dart';
import 'package:dmz/business/mine/model/lose_model.dart';
import 'package:dmz/utils/sp_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class LostUpdataWidgets {
  static Widget inforWidget(List<String> contents, Function(int) click) {
    List<String> titles = [
      '失窃上报类型'.tr,
      '失窃车辆'.tr,
      '失窃时间'.tr,
      '失窃地点'.tr,
      '车主电话'.tr,
    ];

    return Container(
        color: const Color(0xFF434650),
        child: Column(
            mainAxisSize: MainAxisSize.min,
            children: List.generate(
                titles.length,
                (index) => item(titles[index], contents[index],
                        hit: index == titles.length - 1)
                    .addGestureDetector(() => click(index)))));
  }

  static Widget item(String title, String content, {bool hit = false}) {
    return Container(
        child: Column(children: [
      Row(children: [
        DefaultText(title, 40.sp,
                textColor: const Color(0xFFF9F9F9), fontWeight: FontWeight.w700)
            .marginOnly(top: 58.sp, bottom: 58.sp, left: 32.sp, right: 32.sp),
        Expanded(
            child: Container(
                alignment: Alignment.centerRight,
                height: 154.sp,
                color: Colors.transparent,
                child: DefaultText(content, 32.sp,
                    textColor: const Color(0xFFF9F9F9)))),
        if (!hit)
          Image.asset('assets/images/home/set/right_icon.png',
                  width: 18.sp, height: 36.sp)
              .marginOnly(left: 20.sp)
      ]).marginOnly(right: 32.sp),
      if (!hit)
        Container(height: 2.sp, color: const Color(0xFFD8D8D8))
            .marginOnly(left: 32.sp, right: 32.sp)
    ]));
  }

  static Widget bottomWidget(bool isUpdataed, Function didUpdata) {
    return Column(mainAxisSize: MainAxisSize.min, children: [
      DefaultText(isUpdataed ? '解除失窃后将自动解锁车辆'.tr : '失窃上报后将自动锁定车辆'.tr, 32.sp,
              textColor: const Color(0xB3FFFFFF))
          .marginOnly(bottom: 32.sp),
      DefaultBtn(
          click: () => didUpdata(), title: isUpdataed ? '解除失窃'.tr : '确认上报'.tr)
    ]).marginOnly(bottom: 112.sp);
  }
}

class LoseHistoryWidget extends StatefulWidget {
  const LoseHistoryWidget({super.key});

  @override
  State<LoseHistoryWidget> createState() => _LoseHistoryWidgetState();
}

class _LoseHistoryWidgetState extends State<LoseHistoryWidget> {
  final ScrollController _scrollController = ScrollController();
  bool _isLoadingMore = false;
  int index = 1;

  Future<void> _loadMore() async {
    var controller = Get.find<LostUpdataController>();
    if (_isLoadingMore) return;
    if ((controller.loseList?.length ?? 0) % 20 > 0) return;

    _isLoadingMore = true;
    index++;
    await controller.getLoseListData(index);
    _isLoadingMore = false;
  }

  @override
  void initState() {
    super.initState();
    _scrollController.addListener(_scrollListener);
  }

  void _scrollListener() {
    if (_scrollController.position.extentAfter < 500) {
      _loadMore();
    }
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<LostUpdataController>(
        id: 'loseList',
        builder: (controller) {
          return RefreshIndicator(
              color: const Color(0xFF999999),
              onRefresh: () => controller.getLoseListData(index, reload: true),
              child: ListView.builder(
                  itemCount: controller.loseList!.length,
                  itemBuilder: (context, index) {
                    return item(controller.loseList![index]);
                  }));
        });
  }

  item(LoseModel data) {
    return Stack(alignment: Alignment.topRight, children: [
      Container(
              alignment: Alignment.center,
              color: const Color(0xFF434650),
              child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(children: [
                      data.lose_type == 1
                          ? DefaultNetWorkImage(data.ebike_image ?? '',
                              width: 150.sp, height: 180.sp)
                          : Image.asset('assets/images/user/battery_image.png',
                                  width: 150.sp, height: 180.sp)
                              .marginOnly(left: 32.sp),
                      Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            DefaultText(data.ebike_name ?? '', 48.sp,
                                    textColor: const Color(0xFFF9F9F9))
                                .marginOnly(bottom: 20.sp),
                            DefaultText(
                                    '${'上报时间'.tr} ${SPUtils.timeIntToTimeStr(data.updated_at ?? 0)}',
                                    28.sp,
                                    textColor: const Color(0xFF999999))
                                .marginOnly(bottom: 20.sp),
                            Container(
                                height: 112.sp,
                                child: DefaultText(
                                    '${'上报地点'.tr} ${data.addr}', 28.sp,
                                    textColor: const Color(0xFF999999)))
                          ]).marginOnly(left: 54.sp),
                    ]),
                    Container(height: 2.sp, color: const Color(0xFFD8D8D8)),
                    DefaultText(data.lock_status == 1 ? '锁定中'.tr : '解除失窃成功'.tr,
                            28.sp,
                            textColor: const Color(0xFF23C565))
                        .marginOnly(top: 24.sp, left: 32.sp)
                  ]).marginOnly(top: 120.sp, bottom: 24.sp))
          .didClipRRectAll(56.sp),
      Container(
          padding: EdgeInsets.only(
              left: 52.sp, right: 52.sp, top: 22.sp, bottom: 22.sp),
          decoration: BoxDecoration(
            color: const Color(0xFF5AD019),
            borderRadius: BorderRadius.only(
                topRight: Radius.circular(56.sp),
                bottomLeft: Radius.circular(56.sp)), // 设置圆角半径
          ),
          child: DefaultText(data.lose_type == 1 ? '车辆失窃'.tr : '电池失窃'.tr, 28.sp,
              textColor: Colors.white))
    ]).marginOnly(left: 32.sp, right: 32.sp);
  }
}
